package org.example.myleet.p997;

public class Solution {

    public int findJudge(int n, int[][] trust) {
        int[] inDegree = new int[n + 1];
        int[] outDegree = new int[n + 1];
        for (int[] t : trust) {
            ++outDegree[t[0]];
            ++inDegree[t[1]];
        }
        for (int i = 1; i <= n; ++i) {
            if (outDegree[i] == 0 && inDegree[i] == n - 1) {
                return i;
            }
        }
        return -1;
    }

//    public int findJudge(int n, int[][] trust) {
//        int candidateTrustCount = n;
//        int candidate = -1;
//        boolean[] visited = new boolean[n + 1];
//        int[] trustCounts = new int[n + 1];
//        for (int[] t : trust) {
//            if (!visited[t[0]]) {
//                --candidateTrustCount;
//            }
//            visited[t[0]] = true;
//            if (!visited[t[1]]) {
//                candidate = t[1];
//            }
//            ++trustCounts[t[1]];
//        }
//        if (candidateTrustCount == 1) {
//            if (n == 1) {
//                return 1;
//            }
//            if (!visited[candidate] && trustCounts[candidate] == n - 1) {
//                return candidate;
//            }
//        }
//        return -1;
//    }
}
